package com.example.popmp3.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
//import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.DialogInterface.OnClickListener;

import com.example.popmp3.R;
import com.example.popmp3.dao.LoveListDAO;
import com.example.popmp3.dao.MusicListDAO;
import com.example.popmp3.model.Tb_lovelist;
import com.example.popmp3.model.Tb_musiclist;

import java.util.List;

/**
 * Created by Administrator on 2016/3/4.
 */
public class LoveList extends Activity implements View.OnClickListener{
    public static final String FLAG = "id";
    Button btsList,btlList,newList;
    ListView listView2;
    LoveListDAO loveListDAO = new LoveListDAO(LoveList.this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lovelist);

        btsList = (Button) findViewById(R.id.btsList);
        btlList = (Button) findViewById(R.id.btlList);
        newList = (Button) findViewById(R.id.newList);
        listView2=(ListView)findViewById(R.id.listView2);
        ShowInfo(R.id.btlList);
        btsList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoveList.this, MainActivity.class);
                startActivity(intent);
            }
        });
        btlList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowInfo(R.id.btlList);   }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               String strInfo = String.valueOf(((TextView) view).getText());// 记录单击的项信息
                String loveid = strInfo.substring(0, strInfo.indexOf(' '));// 从项信息中截取编号
                Intent intent = new Intent(LoveList.this, SingleList.class);
                intent.putExtra("id", loveid);
                startActivity(intent);
            }
        });

        newList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               final EditText et = new EditText(LoveList.this);
                AlertDialog alert = new AlertDialog.Builder(LoveList.this).create();
                alert.setIcon(R.drawable.advise);	//设置对话框的图标
                alert.setTitle("请输入名称");	//设置对话框的标题
                alert.setIcon(android.R.drawable.ic_dialog_info);
                alert.setView(et);

                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(LoveList.this, "您取消建立列表！",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                //添加确定按钮
                alert.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        Tb_lovelist tb_lovelist = new Tb_lovelist(loveListDAO.getMaxId() + 1,
                                et.getText().toString());
                        loveListDAO.add(tb_lovelist);
                        Toast.makeText(LoveList.this, "新建列表成功！",
                                Toast.LENGTH_SHORT).show();
                        //loveListDAO.update(tb_lovelist);

                    }
                });
                alert.show(); // 创建对话框并显示

            }
        });
    }
    public void ShowInfo(int id) {
        String[] strInfos = null;
        ArrayAdapter<String> arrayAdapter = null;// 创建ArrayAdapter对象

        // 获取所有收入信息，并存储到List泛型集合中
        List<Tb_lovelist> tb_lovelists = loveListDAO.getScrollData(0,
                (int) loveListDAO.getCount());
        strInfos = new String[tb_lovelists.size()];// 设置字符串数组的长度
        int m = 0;// 定义一个开始标识
        for (Tb_lovelist tb_lovelist : tb_lovelists) {// 遍历List泛型集合
            // 将信息组合成一个字符串，存储到字符串数组的相应位置
            strInfos[m] = tb_lovelist.getid() + " " + tb_lovelist.getName();
            m++;// 标识加1
        }
        // 使用字符串数组初始化ArrayAdapter对象
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strInfos);
        listView2.setAdapter(arrayAdapter);// 为ListView列表设置数据源
    }
    @Override
    public void onClick(View v) {

    }
}

